package com.zprmb.udzt;

import java.text.DecimalFormat;
import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.adapter.Ex_gold_Adapter;
import com.zprmb.udzt.bean.Myexgold;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.view.WaveHelper;
import com.zprmb.udzt.view.WaveView;
/**
 * 我的体验金
 * @author shenshao
 *
 */
public class My_ex_gold extends BaseActivity implements OnRefreshListener2<ListView>,OnClickListener{
	private PullToRefreshListView lv_product_fragment;
	private int page=1;
	private ListView actualListView;
	private ArrayList<Myexgold> list = new ArrayList<Myexgold>();
	private ArrayList<Myexgold> list2 = new ArrayList<Myexgold>();
	private TextView tv_amount,tv_due,tv_holdamount,yuqi,time;
	private Ex_gold_Adapter adapter;
	private ImageView img_back,img_info;
	DecimalFormat df = new DecimalFormat("###0.00");
	private RelativeLayout rl_back;
	private WaveView wv;
	private WaveHelper helper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_ex_gold);
		 BbtApplication.getInstance().addActvity(this);
		lv_product_fragment = (PullToRefreshListView)findViewById(R.id.lv);
		tv_amount=(TextView)findViewById(R.id.ex_amount);
		tv_due=(TextView)findViewById(R.id.totaldueInterest);
		tv_holdamount=(TextView)findViewById(R.id.chiyou);
		yuqi=(TextView)findViewById(R.id.yuqi);
		img_info=(ImageView)findViewById(R.id.img_info);
		time=(TextView)findViewById(R.id.time);
		img_back=(ImageView)findViewById(R.id.img_back);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		wv=(WaveView)findViewById(R.id.product_wave);
		helper=new WaveHelper(wv);
		helper.start();
		rl_back.setOnClickListener(this);
		img_back.setOnClickListener(this);
		actualListView = lv_product_fragment.getRefreshableView();
		registerForContextMenu(actualListView);
		lv_product_fragment.setOnRefreshListener(this);
		img_info.setOnClickListener(this);
		lv_product_fragment.setMode(Mode.BOTH);
		initData();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
		helper.cancel();
	}
	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("体验金", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					JSONObject json1=jsonObject.optJSONObject("data");
					tv_amount.setText(df.format(Double.parseDouble(json1.optString("totalReceivedInterest"))));
					tv_due.setText("待收收益(元):"+df.format(Double.parseDouble(json1.optString("totalDueInterest"))));
					tv_holdamount.setText(df.format(Double.parseDouble(json1.optString("expTotalAmount"))));
					yuqi.setText(df.format(Double.parseDouble(json1.optString("fixedRateShow")))+"%");
					time.setText(json1.optString("expDaysShow")+"天");
					Gson gson=new Gson();
					if (!jsonObject.optString("data").equals("null")) {
						list=gson.fromJson(json1.optString("ueList"), new TypeToken<ArrayList<Myexgold>>() {
							}.getType());
						
					}
					list2.addAll(list);
					adapter=new Ex_gold_Adapter(My_ex_gold.this, list2);
					lv_product_fragment.setAdapter(adapter);
					adapter.notifyDataSetChanged();
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(My_ex_gold.this, Constant.URL+"/api/v5/user/getExperienceList?pageNo="+page+"&pageSize=10", "");
	}
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			list.clear();
			if(msg.what==1){
				page=1;
				list2.clear();
				initData();
			}else{
				page+=1;
				initData();
				
				
			}
			Toast.makeText(My_ex_gold.this, "刷新成功", 2000).show();
			lv_product_fragment.onRefreshComplete();
		};
	};
	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(2);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.img_back:
			finish();
			break;
		case R.id.img_info:
			Intent intent=new Intent(My_ex_gold.this,ExperienceGold_info.class);
			startActivity(intent);
			break;
		case R.id.rl_back:
			finish();
			break;
		}
	}

}
